import 'package:flutter/material.dart';
import '../util/DataUtils.dart';
import '../util/ToastUtils.dart';
import '../pages/login.dart';
import '../pages/bottomNav.dart';

class Welcome extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            title: '奇妙时代',
            home: new WelcomePage(),
        );
    }
}

class WelcomePageState extends State<WelcomePage> {
    var _relname = 'Welcome';
    var _icon = 'https://dares.oss-cn-shenzhen.aliyuncs.com/competmp/user_icon.png';
    
    Widget build(BuildContext context) {
        DataUtils.getUserInfo().then((userInfo) {
            if(userInfo != null) {
                setState(() {
                    _relname = 'Welcome, ' + userInfo.relname;
                    if(userInfo.icon.isNotEmpty) {
                        _icon = userInfo.icon;
                    }
                });
            }
        });
        
        return new Scaffold (
            appBar: new AppBar(
                title: new Text('奇妙时代'),
            ),
            body: new Container(
                margin: const EdgeInsets.fromLTRB(0.0, 100.0, 0.0, 0.0),
                child: Column(
                    children: <Widget>[
                        new Center(
                            child: new ClipOval(
                                child: new Image(
                                    image: NetworkImage(_icon),
                                    width: 100.0,
                                    height: 100.0,
                                ),
                            ),
                        ),
                        new Center(
                            child: new Text(_relname),
                        ),
                        new Center(
                            child: FlatButton(
                                child: new Text('退出登录'),
                                onPressed: () {
                                    print('===退出登录？===');
                                    ToastUtils.showConfirmDialog(context, '确认要退出吗？', (){
                                        print('===------>>>确认===');
                                        DataUtils.clearLoginInfo();
                                        ToastUtils.showMsg('已退出');
                                        Navigator.of(context).pushAndRemoveUntil(
                                            new MaterialPageRoute(builder: (context) => new LoginPage()
                                            ), (route) => route == null);
                                    });
                                },
                            ),
                        ),
                    ],
                ),
            ),
            bottomNavigationBar: new BottomNav(2),
        );
    }
}

class WelcomePage extends StatefulWidget {
    @override
    createState() => new WelcomePageState();
}